﻿namespace Amazon.Checkout
{
    using Models;

    using Orchard.ContentManagement.MetaData;
    using Orchard.Core.Contents.Extensions;
    using Orchard.Data.Migration;

    public class CheckoutDataMigration : DataMigrationImpl
    {
        public int Create()
        {
            SchemaBuilder.CreateTable(
                    typeof(SiteSettingsPartRecord).Name,
                    table => table.ContentPartRecord()
                                   .Column<string>("MerchantId")
                                   .Column<string>("AwsAccessKeyId")
                                   .Column<string>("SecretKeyId")
                                   .Column<string>("AssociateTag")
                                   .Column<int>("PromotionId", column => column.Nullable())
                                   .Column<int>("TaxId", column => column.Nullable())
                                   .Column<string>("CurrencyCode")
                                   .Column<string>("LengthUnitOfMeasure")
                                   .Column<string>("VolumeUnitOfMeasure")
                                   .Column<string>("WeightUnitOfMeasure")
                                   .Column<string>("JewelryWeightUnitOfMeasure")
                                   .Column<string>("DegreeUnitOfMeasure")
                                   .Column<string>("MemorySizeUnitOfMeasure")
                                   .Column<string>("FrequencyUnitOfMeasure")
                                   .Column<string>("AmperageUnitOfMeasure")
                                   .Column<string>("TimeUnitOfMeasure")
                                   .Column<string>("DateUnitOfMeasure")
                                   .Column<string>("AssemblyTimeUnitOfMeasure")
                                   .Column<string>("AgeRecommendedUnitOfMeasure")
                                   .Column<string>("BatteryPowerUnitOfMeasure")
                                   .Column<string>("VoltageUnitOfMeasure")
                                   .Column<string>("WattageUnitOfMeasure")
                                   .Column<string>("TemperatureRatingUnitOfMeasure")
                                   .Column<string>("ClothingSizeUnitOfMeasure"));

            SchemaBuilder.CreateTable(
                    typeof(CheckoutByAmazonButtonPartRecord).Name,
                    table => table.ContentPartRecord()
                                  .Column<string>("UpsellingProducts", column => column.Unlimited())
                                  .Column<string>("InclusionCategories", column => column.Unlimited())
                                  .Column<string>("ExclusionCategories", column => column.Unlimited())
                                  .Column<string>("UpsellType")
                                  .Column<int>("PromotionId", column => column.Nullable())
                                  .Column<int>("TaxId", column => column.Nullable()));

            SchemaBuilder.CreateTable(
                    typeof(PromotionRecord).Name,
                    table => table.Column<int>("Id", column => column.PrimaryKey().Identity())
                                  .Column<string>("PromotionCode")
                                  .Column<string>("Description", column => column.Unlimited())
                                  .Column<double>("DiscountRate", column => column.Nullable())
                                  .Column<double>("FixedAmountDiscount", column => column.Nullable()));

            SchemaBuilder.CreateTable(
                    typeof(TaxTableRecord).Name,
                    table => table.Column<int>("Id", column => column.PrimaryKey().Identity())
                                  .Column<string>("Name")
                                  .Column<string>("Taxes"));

            SchemaBuilder.CreateTable(
                    typeof(TaxRuleRecord).Name,
                    table => table.Column<int>("Id", column => column.PrimaryKey().Identity())
                                  .Column<string>("RegionType")
                                  .Column<string>("Region")
                                  .Column<decimal>("Rate")
                                  .Column<bool>("IsShippingTaxed"));

            SchemaBuilder.CreateTable(
                    typeof(ShippingRecord).Name,
                    table => table.Column<int>("Id", column => column.PrimaryKey().Identity())
                        .Column<string>("IncludedRegionsString", column => column.Unlimited())
                        .Column<string>("ExcludedRegionsString", column => column.Unlimited())
                        .Column<decimal>("ShipmentBasedShippingRate")
                        .Column<decimal>("WeightBasedShippingRate")
                        .Column<decimal>("ItemQuantityBasedShippingRate")
                        .Column<bool>("PoBoxSupported")
                        .Column<string>("DisplayableShippingLabel")
                        .Column<int>("ServiceLevel"));

            ContentDefinitionManager.AlterPartDefinition(
                typeof(CheckoutByAmazonButtonPart).Name,
                cfg => cfg.WithSetting("CheckoutByAmazonButtonPartSettings.ProductTitleMapping", string.Empty)
                          .WithSetting("CheckoutByAmazonButtonPartSettings.ProductDescriptionMapping", string.Empty)
                          .WithSetting("CheckoutByAmazonButtonPartSettings.ProductPriceMapping", string.Empty)
                          .WithSetting("CheckoutByAmazonButtonPartSettings.ProductSkuMapping", string.Empty)
                          .WithSetting("CheckoutByAmazonButtonPartSettings.ExpressCheckout", "true")
                          .Attachable());

            ContentDefinitionManager.AlterPartDefinition(typeof(ShoppingCartPart).Name, cfg => cfg.Attachable());

            ContentDefinitionManager.AlterTypeDefinition(
                typeof(ShoppingCartPart).Name,
                cfg => cfg.WithPart("ShoppingCartPart")
                          .WithPart("WidgetPart")
                          .WithPart("CommonPart")
                          .WithSetting("Stereotype", "Widget"));
            return 1;
        }

        public int UpdateFrom1()
        {
            SchemaBuilder.AlterTable(
                typeof(CheckoutByAmazonButtonPartRecord).Name,
                table => table.AddColumn<string>("ShippingMethods", column => column.Unlimited()));
            
            return 2;
        }

        public int UpdateFrom2()
        {
            SchemaBuilder.AlterTable(
                typeof(ShippingRecord).Name,
                table => table.AddColumn<string>("ShipmentRateType"));
            
            return 3;
        }

        public int UpdateFrom3()
        {
            SchemaBuilder.AlterTable(
                typeof(PromotionRecord).Name,
                table => table.AddColumn<string>("PromotionType"));
            
            return 4;
        }
    }
}